package com.example.clxy.service.impl;

import com.example.clxy.dao.SycourseMapper;
import com.example.clxy.entity.Sycourse;
import com.example.clxy.result.PageResult;
import com.example.clxy.result.WebRestResult;
import com.example.clxy.result.business.PageResult.SycoursePageResult;
import com.example.clxy.result.business.SycourseResult;
import com.example.clxy.service.SycourseService;
import com.example.clxy.util.UUIDUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @Author liuhaoqi
 * @Date 2020/8/4 17:32
 * @Version 1.0
 */
@Service
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class SycourseServiceImpl implements SycourseService {

    @Autowired
    SycourseMapper sycourseMapper;

    @Override
    public PageResult<SycoursePageResult> getSycourseList(String sycoursecode, String sycoursename, String credit, String sycourseintro, String sycoursetype, String sycourseunit, int page, int pagesize) {
        PageResult<SycoursePageResult> result = new PageResult<>();
        int begin = (page - 1) * pagesize;
        int end = pagesize;

        List<SycoursePageResult> list = sycourseMapper.getSycourseList(sycoursecode, sycoursename, credit, sycourseintro, sycoursetype, sycourseunit, begin, end, pagesize);
        if (list.size() > 0) {
            result.setTotal(list.get(0).getCnt());
            result.setPageCount(list.get(0).getPage());
            result.setPageIndex(page);
            result.setPageSize(pagesize);
            result.setRows(list);
            result.setResult(WebRestResult.SUCCESS);
            result.setMessage("获取实验课程列表成功！");
        } else {
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("获取实验课程列表失败！");
        }
        return result;
    }

    @Override
    public WebRestResult addSycourse(Sycourse sycourse) {
        WebRestResult result = new WebRestResult();
        sycourse.setSycourseid(UUIDUtil.randomUUID());
        sycourse.setCreatedTime(new Date());
        sycourse.setUpdatedTime(new Date());
        sycourse.setFlag("0");
        if (sycourseMapper.insertSelective(sycourse) == 1) {
            result.setResult(WebRestResult.SUCCESS);
            result.setMessage("新增实验课程成功！");
        } else {
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("新增实验课程失败");
        }
        return result;
    }

    @Override
    public WebRestResult updateSycourse(Sycourse sycourse) {
        WebRestResult result = new WebRestResult();
        sycourse.setUpdatedTime(new Date());
        if (sycourseMapper.updateByPrimaryKeySelective(sycourse) == 1) {
            result.setResult(WebRestResult.SUCCESS);
            result.setMessage("更新实验课程成功！");
        } else {
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("更新实验课程失败");
        }
        return result;
    }

    @Override
    public WebRestResult deleteSycourse(String sycourseid) {
        WebRestResult result = new WebRestResult();
        if (sycourseMapper.deleteSycourse(sycourseid) == 1) {
            result.setResult(WebRestResult.SUCCESS);
            result.setMessage("删除实验课程成功！");
        } else {
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("删除实验课程失败！");
        }
        return result;
    }

    @Override
    public SycourseResult getSycourseById(String sycourseid) {
        SycourseResult result = new SycourseResult();
        SycourseResult sycourse = sycourseMapper.getSycourseById(sycourseid);
        if (sycourse != null) {
            BeanUtils.copyProperties(sycourse, result);
            result.setResult(WebRestResult.SUCCESS);
            result.setMessage("获取单个实验课程信息成功");
        } else {
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("获取单个实验课程信息失败");
        }
        return result;
    }
}
